#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
#include "../nstream.h"

using namespace krryn::core::io;

// we need this little helper because istreams discard whitespace
// this can't be solved by setting the stream / buffer to binary
// because std::istream_iterator uses the >> operator which returns
// a formatted result, instead of the 'raw' result you'd get with get()
class iowhitespace{
public:
	friend std::istream& operator>>(std::istream &a_Stream, iowhitespace &a_String){
		return std::getline(a_Stream >> std::ws, a_String.m_Content);
	}

	friend std::ostream& operator<<(std::ostream &a_Stream, const iowhitespace &a_String){
		// we need to output a newline here because getline uses 
		// \n as a delimiter and then inhalates the delimiter
		return a_Stream << a_String.m_Content << std::endl;
	}
private:
   std::string m_Content;
};

int main(int argc, char **argv){
	std::cout 
		<< "Welcome to krryn debug server 1.0" << std::endl
		<< "Awaiting incoming connections" << std::endl << std::endl;

	while(true){
		instream is;

		while(is.good()){
			std::copy(
				std::istream_iterator<iowhitespace>(is),
				std::istream_iterator<iowhitespace>(),
				std::ostream_iterator<iowhitespace>(std::cout, ""));
		}

		std::cout << std::endl << "Connection ended" << std::endl << std::endl;
	}
}